/**
 * @author 董一华
 * @since 1.00
 * @version 1.00
 * 
 * 下拉框通用控件
 * 使用说明：
 * jsp中使用范例(最常用模式)：<htdz-tag:SelectTag name="xxx" map="${yyy}" selected="${zzz}"/>
 * 参数说明：
 * 1. name为必填项
 * 2. id可以不写,缺省值为name的值
 * 3. onchange可以不写，缺省值为无
 * 4. firstoption为首个下拉框的text。可以不写，缺省值为"请选择"；
 *    如果firstoption为MyConstant.SELECT_FIRST_OPTION_NO，则表示此下拉框不需要添加额外option
 *    如果firstoption为MyConstant.SELECT_FIRST_OPTION_YES_OR_NO_ALL = "-998";// 下拉框控件首个option：当map值只有一个时，则不显示"全部"或"请选择"；如果map值为空或超过1个时，则显示"全部"
 *    如果firstoption为MyConstant.SELECT_FIRST_OPTION_YES_OR_NO_SELECT = "-997";// 下拉框控件首个option：当map值只有一个时，则不显示"全部"或"请选择"；如果map值为空或超过1个时，则显示"请选择"
 * 5. firstvalue为首个下拉框的value。可以不写，缺省值为"MyConstant.SELECT_FIRST_VALUE"
 * 6. selected为下拉框选中的值，可以不写。
 * 7. disabled：是否禁用true/false，缺省值为false
 * 8. map：LinkedHashMap集合数据，为可填项。
 * 9. group：用`(ESC键下面的按键)间隔的字符串替代map。格式：value1`option1`value2`option2.....，必须成对出现，否则视为无效。比如：1`需要`0`不需要
 *    当group和map只能选择其中一项，如果两者共存，则选择有值的一项填充到下拉框中。
 * 修订列表：v1.00 2010-04-18 
 * 			修订人：董一华 
 * 			修订内容：初始版本
 * 
 *          v1.01 2011-11-15 
 * 			修订人：董一华 
 * 			修订内容：新增属性：
 * 
 */

package com.zjht.gmcc.bean.tag;

import java.util.Iterator;
import java.util.Map;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

import com.zjht.gmcc.bean.util.MyConstant;
import com.zjht.gmcc.bean.util.MyFormat;

public class SelectTag extends TagSupport {
	private static final long serialVersionUID = 7343619033252888637L;
	private String name;
	private String id;
	private String firstoption;
	private String firstvalue;
	private String onchange;
	private String selected;
	private boolean disabled;
	private Map<String, String> map;
	private String group;

	public SelectTag() {
	}

	@SuppressWarnings("static-access")
	public int doStartTag() throws JspException {
		String name = this.name;
		String id = this.id;
		String firstoption = this.firstoption;
		String firstvalue = this.firstvalue;
		String onchange = this.onchange;
		String selected = this.selected;
		boolean disabled = this.disabled;
		boolean hasFirstOption = true;
		int mapsize = 0;
		String group = this.group;// 用`间隔
		String groups[] = null;
		int groupsize = 0;

		if (name == null || name.length() == 0) {
			throw new JspException("下拉框name不能为空！");
		}
		if (map != null) {
			mapsize = map.size();
		}
		if (mapsize == 0 && !MyFormat.isStrNull(group)) {
			groups = group.split("`");
			groupsize = groups.length;
			if (groupsize > 0) {
				if (groupsize % 2 != 0) {// 如果不是成对出现，则无效
					groupsize = 0;
				}
			}
		}

		StringBuilder html = new StringBuilder();
		html.append("<select name=\"");
		html.append(name);
		html.append("\"	id=\"");
		if (id != null && id.length() > 0)
			html.append(id);
		else
			html.append(name);
		html.append("\" ");
		if (onchange != null && onchange.length() > 0) {
			html.append("onchange=\"");
			html.append(onchange);
			html.append("\"");
		}
		if (disabled)// 禁用
			html.append(" disabled ");
		html.append(">");
		if (MyConstant.SELECT_FIRST_OPTION_NO.equals(firstoption)) {
			hasFirstOption = false;
		} else if (MyConstant.SELECT_FIRST_OPTION_YES_OR_NO_ALL.equals(firstoption)
				|| MyConstant.SELECT_FIRST_OPTION_YES_OR_NO_SELECT.equals(firstoption)) {
			if (mapsize == 1 || (mapsize == 0 && groupsize == 2)) {
				hasFirstOption = false;
			} else {
				firstoption = MyConstant.SELECT_FIRST_OPTION_YES_OR_NO_ALL.equals(firstoption) ? "全部" : "请选择";
			}
		}
		if (hasFirstOption) {
			if (MyFormat.isStrNull(firstoption))
				firstoption = "请选择";

			if (MyFormat.isStrNull(firstvalue))
				firstvalue = MyConstant.SELECT_FIRST_VALUE;

			html.append("<option value=\"");
			html.append(firstvalue);
			html.append("\">");
			html.append(firstoption);
			html.append("</option>");
		}
		try {
			if (mapsize > 0) {
				Iterator<Map.Entry<String, String>> iterator = map.entrySet().iterator();
				Object key = null;
				Object value = null;
				Map.Entry<String, String> entry = null;
				while (iterator.hasNext()) {
					entry = iterator.next();
					key = entry.getKey();
					value = entry.getValue();
					html.append("<option value=\"");
					html.append(key);
					html.append("\" ");
					if (!MyFormat.isStrNull(selected) && key != null && key.equals(selected))
						html.append("selected");
					html.append(">");
					html.append(value);
					html.append("</option>");
				}
			} else if (groupsize > 0) {
				String value = "";
				for (int i = 0; i < groupsize;) {
					value = groups[i++];
					html.append("<option value=\"");
					html.append(value);
					html.append("\" ");
					if (!MyFormat.isStrNull(selected) && value != null && value.equals(selected))
						html.append("selected");
					html.append(">");
					html.append(groups[i++]);
					html.append("</option>");
				}
			}
			html.append("</select>");
			pageContext.getOut().print(html.toString());
		} catch (Exception e) {
			throw new JspException(e.getMessage());
		}
		html = null;
		return this.SKIP_BODY;
	}

	public void setName(String newName) {
		name = newName;
	}

	public void setId(String newId) {
		id = newId;
	}

	public void setFirstoption(String firstoption) {
		this.firstoption = firstoption;
	}

	public void setOnchange(String onchange) {
		this.onchange = onchange;
	}

	public void setSelected(String selected) {
		this.selected = selected;
	}

	public void setSelected(int selected) {
		this.selected = String.valueOf(selected);
	}

	public void setFirstvalue(String firstvalue) {
		this.firstvalue = firstvalue;
	}

	public void setDisabled(boolean disabled) {
		this.disabled = disabled;
	}

	public void setDisabled(String disabled) {
		this.disabled = (disabled != null && "true".equals(disabled.trim().toLowerCase()));
	}

	public void setMap(Map<String, String> map) {
		this.map = map;
	}

	public void setGroup(String group) {
		this.group = group;
	}
}
